package com.ruoyi.app.service.impl

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.app.domain.vo.AdvertisingVo
import com.ruoyi.app.domain.vo.BootPageVo
import com.ruoyi.app.service.AdvertisingApiService
import com.ruoyi.back.domain.BootPage
import com.ruoyi.back.domain.OpenAdvertising
import com.ruoyi.back.service.BootPageService
import com.ruoyi.back.service.OpenAdvertisingService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class AdvertisingApiServiceImpl : AdvertisingApiService {

    @Autowired
    private lateinit var bootPageService: BootPageService

    @Autowired
    private lateinit var advertisingService: OpenAdvertisingService

    override fun getBootPageList(): Data<List<BootPageVo>> =
        bootPageService.list(
            KtQueryWrapper(BootPage::class.java)
                .eq(BootPage::isShow, true)
                .orderByDesc(BootPage::createTime)
        ).takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取启动页成功", map {
                    BootPageVo().apply { BeanUtil.copyProperties(it, this) }
                })
            } ?: Ok("暂无数据", null)

    override fun getAdvertisingList(type: Int): Data<List<AdvertisingVo>> =
        advertisingService.list(
            KtQueryWrapper(OpenAdvertising::class.java)
                .eq(OpenAdvertising::advertisingType, type)
                .eq(OpenAdvertising::isShow, true)
                .orderByDesc(OpenAdvertising::createTime)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取广告列表成功", map {
                    AdvertisingVo().apply { BeanUtil.copyProperties(it, this) }
                })
            } ?: Ok("暂无数据", null)
}